package com.taobaolite.user;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import org.apache.catalina.connector.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping(path = "/api/v1/users")
//@SaCheckLogin 在需要检测登录的方法或类之前写这个
public class UserController {
    private final UserService userService;
    private final UserRepository userRepo;

    @Autowired
    public UserController(UserService userService, UserRepository userRepo) {
        this.userService = userService;
        this.userRepo = userRepo;
    }

    @PostMapping("/login")
    public RespBody doLogin(@RequestBody FormBody formBody) {
        String username = formBody.getUsername();
        String password = formBody.getPassword();

        System.out.println(formBody);

        UserBody userResponse = userService.loginCheck(username,password);

        StpUtil.login(userResponse.getUserId());//在sa-token框架里登录

        return userResponse.toRespBody(StpUtil.getTokenName(), StpUtil.getTokenValue());
//       return  userResponse;
    }

    @PostMapping("/loginOut")
    public void loginOut() {
        User.checkToken();
        StpUtil.logout();
//        return HttpStatus.OK;
    }

    @GetMapping("{userId}")
    public List<UserBody> getUser(@PathVariable("userId") Long userId) {
        return userService.getUser(userId);
    }

//    @GetMapping("{userId}")
//    public ResponseEntity<User> getUser(@PathVariable("userId") Long userId) {
//        return ResponseEntity.ok().body(userRepo.findById(userId)
//                .orElseThrow(() -> new ResponseStatusException(
//                        HttpStatus.NOT_FOUND,
//                        "User not found."
//                )));
//    }

    @PostMapping()
    public UserBody addUser(@RequestBody UserBody userBody) {
//        userRequest.getGuestRequest();
//        GuestRequst guest = new GuestRequest((GuestRequest)userRequest);

        if (!userBody.isLegalRequest()) {
            throw new ResponseStatusException(
                    HttpStatus.NOT_ACCEPTABLE,
                    "User request is illegal."
            );
        }

        switch (userBody.getType()) {
            case ADMIN: userService.addAdmin(userBody);
            break;
            case GUEST: userService.addGuest(userBody);
            break;
            case STORE: userService.addStore(userBody);
            break;
        }
        return userBody;
    }

    @PutMapping(path = "{userid}")
    public UserBody updateUser(@PathVariable("userid") Long userId, @RequestBody UserBody userBody) {

        if (!userBody.isLegalRequest()) {
            throw new ResponseStatusException(
                    HttpStatus.NOT_ACCEPTABLE,
                    "User request is illegal."
            );
        }

        // Pass userId separately because it could be null in userRequest.
        userService.updateUser(userId, userBody);

        return userBody;
    }

    // TODO: UserDetail
    // TODO: 根据UserDetail修改API
}
